package com.ruoyi.gqgl.gqzc.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.gqgl.gqzc.domain.Gqzc;
import com.ruoyi.gqgl.gqzc.domain.GqzcFj;
import com.ruoyi.gqgl.gqzc.service.IGqzcService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.List;

/**
 * 高企政策Controller
 *
 * @author huacai
 * @date 2025-02-12
 */
@RestController
@RequestMapping("/gqgl/gqzc")
public class GqzcController extends BaseController {
    @Resource
    private IGqzcService gqzcService;

    /**
     * 查询高企政策列表
     */
    @PreAuthorize("@ss.hasPermi('gqgl:gqzc:list')")
    @GetMapping("/list")
    public TableDataInfo list(Gqzc gqzc) {
        startPage();
        List<Gqzc> list = gqzcService.selectGqzcList(gqzc);
        return getDataTable(list);
    }

    /**
     * 导出高企政策列表
     */
    @PreAuthorize("@ss.hasPermi('gqgl:gqzc:export')")
    @Log(title = "高企政策", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Gqzc gqzc) {
        List<Gqzc> list = gqzcService.selectGqzcList(gqzc);
        ExcelUtil<Gqzc> util = new ExcelUtil<Gqzc>(Gqzc.class);
        util.exportExcel(response, list, "高企政策数据");
    }

    /**
     * 下载模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<Gqzc> util = new ExcelUtil<Gqzc>(Gqzc.class);
        util.importTemplateExcel(response, "高企政策数据");
    }

    /**
     * 导入数据
     */
    @Log(title = "高企政策", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('gqgl:gqzc:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<Gqzc> util = new ExcelUtil<Gqzc>(Gqzc.class);
        InputStream inputStream = file.getInputStream();
        List<Gqzc> list = util.importExcel(inputStream);
        inputStream.close();
        int count = gqzcService.batchInsertGqzc(list);
        return AjaxResult.success("导入成功" + count + "条信息！");
    }

    /**
     * 获取高企政策详细信息
     */
    @PreAuthorize("@ss.hasPermi('gqgl:gqzc:query')")
    @GetMapping(value = "/{gqzcId}")
    public AjaxResult getInfo(@PathVariable("gqzcId") String gqzcId) {
        return success(gqzcService.selectGqzcByGqzcId(gqzcId));
    }

    /**
     * 新增高企政策
     */
    @PreAuthorize("@ss.hasPermi('gqgl:gqzc:add')")
    @Log(title = "高企政策", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Gqzc gqzc) {
        gqzc.setUserId(Math.toIntExact(getUserId())); //用户ID
        gqzc.setDeptId(Math.toIntExact(getDeptId())); //部门ID
        gqzc.setCreateBy(getUsername()); //创建人
        gqzc.setIdWhere("存在");
        return toAjax(gqzcService.insertGqzc(gqzc));
    }

    /**
     * 修改高企政策
     */
    @PreAuthorize("@ss.hasPermi('gqgl:gqzc:edit')")
    @Log(title = "高企政策", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Gqzc gqzc) {
        return toAjax(gqzcService.updateGqzc(gqzc));
    }

    /**
     * 删除高企政策
     */
    @PreAuthorize("@ss.hasPermi('gqgl:gqzc:remove')")
    @Log(title = "高企政策", businessType = BusinessType.DELETE)
    @DeleteMapping("/{gqzcIds}")
    public AjaxResult remove(@PathVariable String[] gqzcIds) {
        return toAjax(gqzcService.deleteGqzcByGqzcIds(gqzcIds));
    }

    /**
     * 获得高企政策主键ID (UUID)
     */
    @GetMapping("/getGqZcId")
    public AjaxResult getGqZcId() {
        String getGqZcId = IdUtils.fastSimpleUUID();
        return success(getGqZcId);
    }

    /**
     * 新增附件
     */
    @PostMapping("/add/File/By/subId")
    public AjaxResult insertGqzcFjByGqzcId(@RequestBody GqzcFj gqzcFj) {
        return toAjax(gqzcService.insertGqzcFjByGqzcId(gqzcFj));
    }

    /**
     * 根据ID查询附件列表
     */
    @GetMapping("/get/FileList/By/subId")
    public AjaxResult selectGqzcFjList(String sId) {
        GqzcFj gqzcFj = new GqzcFj();
        gqzcFj.setGqzcId(sId);
        return AjaxResult.success(gqzcService.selectGqzcFjList(gqzcFj));
    }

    /**
     * 删除高企政策附件
     */
    @Log(title = "高企政策附件", businessType = BusinessType.DELETE)
    @DeleteMapping("/fj/{fjIds}")
    public AjaxResult remove(@PathVariable Integer[] fjIds) {
        return toAjax(gqzcService.deleteGqzcFjByFjIds(fjIds));
    }


}
